class Solution4 {
    public String countAndSay(int n) {
        String ret = "1";
        for(int i = 1; i < n; i++) {
            StringBuilder stb = new StringBuilder();
            for(int left = 0,right = 0; right < ret.length(); ) {
                while(right < ret.length() && ret.charAt(left) == ret.charAt(right)) right++;
                stb.append(Integer.toString(right - left));
                stb.append(ret.charAt(left));
                left = right;
            }
            ret = stb.toString();
        }
        return ret;
    }
}